<?php
session_start();
include("../db_connect.php");

if(isset($_GET['command'])){

if(isset($_GET['itemId'])){
$itemId = $_GET['itemId'];
}

	if($_GET['command']== "add"){
		$qty = $_GET['qty'];
		addtocart($itemId,$qty);
	}
	else if($_GET['command']== "delete"){
		remove_product($itemId);
	}
	else if($_GET['command']== "deleteall"){
		unset($_SESSION['cart']);
	}
	else if($_GET['command']== "quanty"){
	
	}
	
}

	function get_product_name($pid){
		$result=mysql_query("select name from tblswaping where id=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
		$row=mysql_fetch_array($result);
		return $row['name'];
	}
	function get_price($pid){
		$result=mysql_query("select price from tblswaping where id=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
		$row=mysql_fetch_array($result);
		return $row['price'];
	}
	function remove_product($pid){
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				unset($_SESSION['cart'][$i]);
				break;
			}
		}
		$_SESSION['cart']=array_values($_SESSION['cart']);
	}
	function get_order_total(){
		$max=count($_SESSION['cart']);
		$sum=0;
		for($i=0;$i<$max;$i++){
			$pid=$_SESSION['cart'][$i]['productid'];
			$q=$_SESSION['cart'][$i]['qty'];
			$price=get_price($pid);
			$sum+=$price*$q;
		}
		return $sum;
	}
	function addtocart($pid,$q){
		
		if($pid<1 or $q<1) return;
		if(isset($_SESSION['cart'])){
			
			if(is_array($_SESSION['cart'])){
				if(product_exists($pid)){
					$x = get_product_name($pid);
					echo "This item ".  $x . " already in your cart";
					return;
				}
				
				$max=count($_SESSION['cart']);
				$_SESSION['cart'][$max]['productid']=$pid;
				$_SESSION['cart'][$max]['qty']=$q;
				
			}
			else{
				$_SESSION['cart']=array();
				$_SESSION['cart'][0]['productid']=$pid;
				$_SESSION['cart'][0]['qty']=$q;
				
			}
		
		}else{
		$_SESSION['cart']=array();
		$_SESSION['cart'][0]['productid']=$pid;
		$_SESSION['cart'][0]['qty']=$q;
		
		}
		
		
	}
	function product_exists($pid){
	
		$pid=intval($pid);
		$max=count($_SESSION['cart']);
		$flag=false;
		for($i=0;$i<$max;$i++){
			if($pid==$_SESSION['cart'][$i]['productid']){
				$flag=true;
				break;
			}
		}
		return $flag;
	}

?>